<template>
  <el-dialog
    :title="title"
    :visible="isShow"
    :close-on-click-modal="false"
    @close="closeDialog"
  >
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="点位名称: " label-width="140px" prop="name">
        <el-input
          v-model="form.name"
          show-word-limit
          maxlength="15"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="所在区域: " label-width="140px" prop="regionId">
        <el-select v-model="form.regionId">
          <el-option
            v-for="item in selectRegionData"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属商圈: " label-width="140px" prop="businessId">
        <el-select v-model="form.businessId">
          <el-option
            v-for="item in BusinessTypeSelectData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="归属合作商: " label-width="140px" prop="ownerId">
        <el-select v-model="form.ownerId">
          <el-option
            v-for="item in PartnerSelectData"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="详细地址: "
        label-width="140px"
        class="last_form_item"
        prop="addr"
      >
        <el-cascader
          v-model="cascader_area"
          :options="areaData"
          @change="handlerArea"
        />
        <el-input
          v-model="form.addr"
          type="textarea"
          :rows="4"
          show-word-limit
          maxlength="60"
          placeholder="请输入"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="footer">
        <el-button class="cancle" type="primary" @click="closeDialog">
          取消
        </el-button>
        <el-button class="confirm" type="primary" @click="submit">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getRegionListApi } from '@/api/pointManage/region'
import {
  getBusinessTypeList,
  addPoint,
  updatePoint
} from '@/api/pointManage/point'
import { partnerListApi } from '@/api/pointManage/partner'
import areas from '@/api/pointManage/area'
export default {
  name: 'DianweiFormDialog',
  data () {
    return {
      title: '',
      isShow: false,
      form: {
        name: '',
        addr: '',
        areaCode: '',
        regionId: '',
        businessId: '',
        ownerId: '',
        createUserId: '1',
        ownerName: ''
      },
      selectRegionData: [],
      BusinessTypeSelectData: [],
      PartnerSelectData: [],
      areaData: [],
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        addr: [{ required: true, message: '请输入', trigger: 'change' }],
        ownerId: [{ required: true, message: '请输入', trigger: 'change' }],
        regionId: [{ required: true, message: '请输入', trigger: 'change' }],
        businessId: [{ required: true, message: '请输入', trigger: 'change' }],
        ownerName: [{ required: true, message: '请输入', trigger: 'change' }]
      },
      cascader_area: ''
    }
  },
  watch: {
    'form.ownerId': {
      handler (val) {
        this.PartnerSelectData.forEach(item => {
          if (item.id === val) {
            this.form.ownerName = item.name
          }
        })
      }
    }
  },
  created () {
    this.getRegionSelectData()
    this.getBusinessTypeSelectData()
    this.getPartnerList()
    this.areaData = areas
  },
  methods: {
    openDialog (type, row) {
      if (type === 'add') {
        this.title = '新增点位'
      } else {
        this.title = '修改点位'
        this.form = row
      }
      this.isShow = true
    },
    closeDialog () {
      this.form = {
        name: '',
        addr: '',
        areaCode: '',
        regionId: '',
        businessId: '',
        ownerId: '',
        createUserId: '1',
        ownerName: ''
      }
      this.cascader_area = ''
      this.$refs.form.resetFields()
      this.isShow = false
    },
    async getRegionSelectData () {
      const { totalCount } = await getRegionListApi()
      const res = await getRegionListApi({ pageSize: totalCount })
      this.selectRegionData = res.currentPageRecords
    },
    async getBusinessTypeSelectData () {
      const res = await getBusinessTypeList()
      this.BusinessTypeSelectData = res
    },
    async getPartnerList () {
      const { totalCount } = await partnerListApi()
      const res = await partnerListApi({ pageSize: totalCount })
      this.PartnerSelectData = res.currentPageRecords
    },
    handlerArea (data) {
      this.form.areaCode = data[data.length - 1]
    },
    async submit () {
      try {
        await this.$refs.form.validate()
        if (!this.form.id) {
          await addPoint(this.form)
          this.$message.success('添加成功')
        } else {
          await updatePoint(this.form)
          this.$message.success('修改成功')
        }
        this.isShow = false
        this.$emit('getNodeList')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 396px;
}
.el-cascader {
  width: 396px;
  margin-bottom: 10px;
}
.el-input {
  width: 396px;
  height: 36px;
  ::v-deep .el-input__inner {
    height: 36px;
  }
}
::v-deep .el-dialog {
  border-radius: 10px;
  width: 630px;
}
::v-deep .el-dialog__body {
  padding: 20px 20px 30px;
}
::v-deep .el-dialog__footer {
  padding: 0;
  padding-bottom: 40px;
}
.last_form_item {
  margin: 0;
}
.footer {
  text-align: center;
}
.cancle {
  background-color: #fbf4f0 !important;
  color: #655b56 !important;
  border: none;
}
.confirm {
  margin-left: 34px !important;
  background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
  border: none;
}
.el-textarea {
  width: 396px;
}
.remark_form_item {
  margin: 0;
}
</style>
